//
//  ModifyValidityView.m
//  fitnessmanage
//
//  Created by 众拳科技 on 2017/11/1.
//  Copyright © 2017年 zongquankeji. All rights reserved.
//

#import "ModifyValidityView.h"
#import "MOFSPickerManager.h"

@implementation ModifyValidityView

-(id)initWithValid:(BOOL)isOpenValid{
    
    self = [super init];
    
    if (self) {
 
        self.backgroundColor = BackGroundColor;
        
        /**
         有效期
         */
        CommentSwitchView *validSwitchView = [[CommentSwitchView alloc] init];
        validSwitchView.titleLabel.text = @"有效期";
        [validSwitchView.titleSwitch setOn:isOpenValid];
        [validSwitchView.titleSwitch addTarget:self action:@selector(validSwitchAction:) forControlEvents:UIControlEventValueChanged];
        [self addSubview:validSwitchView];
        self.validSwitchView = validSwitchView;
        
        if (isOpenValid) {
         
            /**
             开始日期
             */
            CommonBtnView *beginDateBtnView = [[CommonBtnView alloc] init];
            beginDateBtnView.titleLabel.text = @"开始日期";
            beginDateBtnView.choseLabel.text = @"选择日期";
            beginDateBtnView.choseLabel.textColor = PlaceHoldColor;
            beginDateBtnView.choseBtn.tag = 0;
            [beginDateBtnView.choseBtn addTarget:self action:@selector(beginTimeBtnClick:)];
            [self addSubview:beginDateBtnView];
            self.beginDateBtnView = beginDateBtnView;
            
            /**
             结束日期
             */
            CommonBtnView *endDateBtnView = [[CommonBtnView alloc] init];
            endDateBtnView.titleLabel.text = @"结束日期";
            endDateBtnView.choseLabel.text = @"选择日期";
            endDateBtnView.choseLabel.textColor = PlaceHoldColor;
            endDateBtnView.choseBtn.tag = 1;
            [endDateBtnView.choseBtn addTarget:self action:@selector(beginTimeBtnClick:)];
            [self addSubview:endDateBtnView];
            self.endDateBtnView = endDateBtnView;
        }
        
        /**
         是否收费
         */
        CommentSwitchView *chargeSwitchView =  [[CommentSwitchView alloc] init];
        chargeSwitchView.titleLabel.text = @"是否收费";
        [chargeSwitchView.titleSwitch setOn:YES];
        [chargeSwitchView.titleSwitch setOn:isOpenValid];
        [chargeSwitchView.titleSwitch addTarget:self action:@selector(chargeSwitchAction:) forControlEvents:UIControlEventValueChanged];
        [self addSubview:chargeSwitchView];
        self.chargeSwitchView = chargeSwitchView;;
        
        
        /**
         支付方法
         */
        CommonBtnView *chargeStyleBtnView = [[CommonBtnView alloc] init];
        chargeStyleBtnView.titleLabel.text = @"支付方法";
        chargeStyleBtnView.choseLabel.text = @"选择支付方式";
        [self addSubview:chargeStyleBtnView];
        self.chargeStyleBtnView = chargeStyleBtnView;
        
        
        /**
         金额
         */
        CommonInputView *chargeInputView = [[CommonInputView alloc] init];
        chargeInputView.titleLabel.text = @"金额(元)";
        [self addSubview:chargeInputView];
        self.chargeInputView = chargeInputView;
        
        [self setConstraints];
    }
    
    return self;
}

-(void)setConstraints{
    
    /**
     有效期
     */
    [self.validSwitchView mas_remakeConstraints:^(MASConstraintMaker *make) {
       
        make.top.left.width.mas_equalTo(self);
    }];
    
    if (self.validSwitchView.titleSwitch.isOn) {
        
        /**
         开始日期
         */
        [self.beginDateBtnView mas_remakeConstraints:^(MASConstraintMaker *make) {
           
            make.top.mas_equalTo(self.validSwitchView.mas_bottom);
            make.left.width.mas_equalTo(self);
        }];
        
        /**
         结束日期
         */
        [self.endDateBtnView mas_remakeConstraints:^(MASConstraintMaker *make) {
           
            make.top.mas_equalTo(self.beginDateBtnView.mas_bottom);
            make.left.width.mas_equalTo(self);
        }];
        
        /**
         是否收费
         */
        [self.chargeSwitchView mas_remakeConstraints:^(MASConstraintMaker *make) {
           
            make.top.mas_equalTo(self.endDateBtnView.mas_bottom).offset(10);
            make.left.width.mas_equalTo(self);
        }];
    }
    else{
        
        /**
         是否收费
         */
        [self.chargeSwitchView mas_remakeConstraints:^(MASConstraintMaker *make) {
            
            make.top.mas_equalTo(self.validSwitchView.mas_bottom);
            make.left.width.mas_equalTo(self);
        }];
        
    }
    
    if (self.chargeSwitchView.titleSwitch.isOn) {
        
        /**
         支付方法
         */
        [self.chargeStyleBtnView mas_remakeConstraints:^(MASConstraintMaker *make) {
            
            make.top.mas_equalTo(self.chargeSwitchView.mas_bottom);
            make.left.width.mas_equalTo(self);
        }];
        
        
        /**
         金额
         */
        [self.chargeInputView mas_remakeConstraints:^(MASConstraintMaker *make) {
           
            make.top.mas_equalTo(self.chargeStyleBtnView.mas_bottom);
            make.left.width.mas_equalTo(self);
        }];
    }
    
}

-(void)validSwitchAction:(UISwitch *)sender{
    
    UISwitch *switchButton = (UISwitch*)sender;
    
    Boolean isOn = [switchButton isOn];
    
    if (isOn) {
        
        /**
         开始日期
         */
        CommonBtnView *beginDateBtnView = [[CommonBtnView alloc] init];
        beginDateBtnView.titleLabel.text = @"开始日期";
        beginDateBtnView.choseLabel.text = @"选择日期";
        beginDateBtnView.choseLabel.textColor = PlaceHoldColor;
        beginDateBtnView.choseBtn.tag = 0;
        [beginDateBtnView.choseBtn addTarget:self action:@selector(beginTimeBtnClick:)];
        [self addSubview:beginDateBtnView];
        self.beginDateBtnView = beginDateBtnView;
        
        /**
         结束日期
         */
        CommonBtnView *endDateBtnView = [[CommonBtnView alloc] init];
        endDateBtnView.titleLabel.text = @"结束日期";
        endDateBtnView.choseLabel.text = @"选择日期";
        endDateBtnView.choseLabel.textColor = PlaceHoldColor;
        endDateBtnView.choseBtn.tag = 1;
        [endDateBtnView.choseBtn addTarget:self action:@selector(beginTimeBtnClick:)];
        [self addSubview:endDateBtnView];
        self.endDateBtnView = endDateBtnView;
    }
    else{
        
        [self.beginDateBtnView removeFromSuperview];
        self.beginDateBtnView = nil;
        
        [self.endDateBtnView removeFromSuperview];
        self.endDateBtnView = nil;
    }
    
    [self setConstraints];
}


-(void)chargeSwitchAction:(UISwitch *)sender{
    
    UISwitch *switchButton = (UISwitch*)sender;
    
    Boolean isOn = [switchButton isOn];
    
    if (isOn) {
        
        /**
         支付方法
         */
        CommonBtnView *chargeStyleBtnView = [[CommonBtnView alloc] init];
        chargeStyleBtnView.titleLabel.text = @"支付方法";
        chargeStyleBtnView.choseLabel.text = @"选择支付方式";
        [self addSubview:chargeStyleBtnView];
        self.chargeStyleBtnView = chargeStyleBtnView;
        
        
        /**
         金额
         */
        CommonInputView *chargeInputView = [[CommonInputView alloc] init];
        chargeInputView.titleLabel.text = @"金额(元)";
        [self addSubview:chargeInputView];
        self.chargeInputView = chargeInputView;
    }
    else{
        
        [self.chargeStyleBtnView removeFromSuperview];
        self.chargeStyleBtnView = nil;
        
        [self.chargeInputView removeFromSuperview];
        self.chargeInputView = nil;
    }
    
    [self setConstraints];
}

-(void)beginTimeBtnClick:(UIButton *)btn{
    
    [self endEditing:YES];
    
    __weak typeof(self)weakSelf = self;
    
    [[MOFSPickerManager shareManger] showDatePickerWithTag:89 title:@"选择时间" cancelTitle:@"取消" commitTitle:@"确定" datePickerMode:UIDatePickerModeDate commitBlock:^(NSDate *date) {
        
        NSDateFormatter *dateFormatter=[[NSDateFormatter alloc]init];//创建一个日期格式化器
        
        dateFormatter.dateFormat=@"yyyy-MM-dd";//指定转date得日期格式化形式
        
        if (btn.tag == 0) {
            
            weakSelf.beginDateBtnView.choseLabel.text = [dateFormatter stringFromDate:date];
            
            weakSelf.beginDateBtnView.choseLabel.textColor = TFColor;
            
            weakSelf.startTime = [dateFormatter stringFromDate:date];
        }
        else{
            
            weakSelf.endDateBtnView.choseLabel.text = [dateFormatter stringFromDate:date];
            
            weakSelf.endDateBtnView.choseLabel.textColor = TFColor;
            
            weakSelf.endTime = [dateFormatter stringFromDate:date];
        }
        
    } cancelBlock:^{
        
        
    }];
}
@end
